package com.bai.serviceOA.service.impl;


import com.bai.Springsecurity.custom.CustomUser;
import com.bai.Springsecurity.custom.LoginUserInfoHelper;
import com.bai.model.system.SysUser;
import com.bai.serviceOA.mapper.SysUserMapper;
import com.bai.serviceOA.service.SysMenuService;
import com.bai.serviceOA.service.SysUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author bai
 * @since 2023-05-06
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService, UserDetailsService {

    @Autowired
    private SysMenuService sysMenuService;

    //根据用户id修改用户状态
    @Override
    public void updateStatus(Long id, Integer status) {
        //根据用户id查询用户
        SysUser sysUser = baseMapper.selectById(id);
        //设置要修改的状态值
        sysUser.setStatus(status);
        //调用方法进行修改
        baseMapper.updateById(sysUser);
    }

    /**
     * 根据用户名进行查询
     *
     * @param username
     * @return
     */
    @Override
    public SysUser getUserByUserName(String username) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getUsername, username);
        SysUser sysUser = baseMapper.selectOne(wrapper);
        return sysUser;
    }

    /**
     * 获取当前用户信息
     *
     * @return
     */
    @Override
    public Map<String, Object> getCurrentUser() {
        SysUser sysUser = baseMapper.selectById(LoginUserInfoHelper.getUserId());
        Map<String, Object> map = new HashMap<>();
        map.put("name", sysUser.getUsername());
        map.put("phone", sysUser.getPhone());
        return map;
    }


    /**
     * 根据用户名称进行查询
     *
     * @param
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public CustomUser loadUserByUsername(String s) throws UsernameNotFoundException {
        SysUser sysUser = this.getUserByUserName(s);
        if (sysUser == null) {
            throw new UsernameNotFoundException("用户名不存在");
        }
        if (sysUser.getStatus().intValue() == 0) {
            throw new RuntimeException("账号已停用");
        }
        //根据用户id查询用户的权限数据
        List<String> userPermsList = sysMenuService.findUserPermsByUserId(sysUser.getId());
        //封装数据
        List<SimpleGrantedAuthority> authList = new ArrayList<>();
        for (String perms : userPermsList) {
            authList.add(new SimpleGrantedAuthority(perms.trim()));
        }
        return new CustomUser(sysUser, authList);
    }
}
